package com.ly.lypopupwindowdemo.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.database.MyDatabaseHelper;

public class SQLiteDemoActivity extends AppCompatActivity implements View.OnClickListener{

    Button create_database_btn;
    private MyDatabaseHelper dbHelper;
    private static final String TAG = "SQLiteDemoActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_demo);

        dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
        create_database_btn = (Button) findViewById(R.id.id_create_database);
        create_database_btn.setOnClickListener(this);
        findViewById(R.id.id_add_data).setOnClickListener(this);
        findViewById(R.id.id_delete_data).setOnClickListener(this);
        findViewById(R.id.id_query_data).setOnClickListener(this);
        findViewById(R.id.id_Litepal).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_create_database:
                //创建数据库
                dbHelper.getWritableDatabase();
                break;
            case R.id.id_add_data:
            {  //对表中的数据进行操作
                //第一步，获取SQLiteDatabase对象
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                //第二步使用ContentValues对要添加的数据进行组装
                //开始组装第一条数据
                values.put("name","Harry Potter");
                values.put("author","JK.罗琳");
                values.put("pages","100");
                values.put("price","200");
                db.insert("Book",null,values);//插入第一条数据
                //第一个参数，想要往哪个表中插入数据就写哪个表的名字，第二个参数一般不用直接传null第三个参数传入的是ContentValues队形
                values.clear();
                //开始组装第二条数据

                values.put("name","第一行代码");
                values.put("author","郭霖");
                values.put("pages","570");
                values.put("price","70");
                db.insert("Book",null,values);//插入第二条数据

                //第三步，调用insert方法将数据添加到表中
                Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();}
                break;
            case R.id.id_delete_data: {  //删除数据
                //删除页数大于500的书
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages>?", new String[]{"500"});
            }
                break;
            case R.id.id_query_data: {
                //查询数据
                {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    //查询Book表中所有数据
                    Cursor cursor = db.query("Book",null,null,null,null,null,null);

                    //moveToFirst()将数据的指针移动到第一行
                    if (cursor.moveToFirst()){
                        do {
                            //遍历cursor对象，取出数据并打印
                            String name = cursor.getString(cursor.getColumnIndex("name"));
                            String author = cursor.getString(cursor.getColumnIndex("author"));
                            int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                            double price = cursor.getDouble(cursor.getColumnIndex("price"));

                            Log.e(TAG, "onClick: book name is"+name );
                            Log.e(TAG, "onClick: book author is"+author );
                            Log.e(TAG, "onClick: book pages is"+pages );
                            Log.e(TAG, "onClick: book price is"+price );
                        }while (cursor.moveToNext());
                    }
                    cursor.close();
                }
            }
                break;
            case R.id.id_Litepal:
                //打开LitePal练习demo
                startActivity(new Intent(this,LitePalDemoActivity.class));
                break;
            default:
                break;
        }
    }
}
